﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKArchUtils.Utils;
using YJKRevitBase.WinForm;
using YJKRevitView;

namespace YJKRevitFloorModule.CreateFloor
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CreateFloorCmd : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				if (CreateFloorCmd._hWndRevit == null)
				{
					CreateFloorCmd._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				CmdViewType types = (CmdViewType)9;
				if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
				{
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					HideWarningAndErroringKit @object = new HideWarningAndErroringKit(new List<string>
					{
						"Create Floors"
					}, HideWarningAndErroringKit.HideKitEnum.HideWarning);
					commandData.Application.Application.FailuresProcessing += @object.OnFailuresProcessing;
					Result result2 = new UserOperationManager(commandData).SelectGenerationMethod(ref message);
                    if (result2 != Result.Succeeded)
					{
						if (result2 == Autodesk.Revit.UI.Result.Failed)
						{
							YJKMessageBox.Show(message);
						}
						result = result2;
					}
					else
					{
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
				}
			}
			catch (Exception)
			{
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}

		public static WindowHandle _hWndRevit;
	}
}
